package com.gjintel.framework.validateunique.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.validateunique.service.IValidateUniqueService;

/**
 * @description 唯一性校验类
 * @author fengya
 * @date 2015-8-5 下午04:01:25
 */
@Controller
@RequestMapping("/validate")
public class ValidateUniqueController {
	@Autowired
	private IValidateUniqueService validateUniqueService;

	/**
	 * @description 唯一性校验方法
	 * @author fengya
	 * @date 2015-6-11 上午10:35:23
	 * @param @param clsName 进行操作的表名称
	 * @param @param field 进行唯一性校验的字段名称
	 * @param @param value 进行唯一性校验的字段对应的值
	 * @param @return
	 * @return String 如果字段对应的value在表中存在，返回unsuccess,否则success
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/unique")
	@ResponseBody
	public String unique(String tableName, String field, String value, String id) throws UnsupportedEncodingException {
		if (value != null) {
			value = URLDecoder.decode(value, "UTF-8");
			if (this.validateUniqueService.unique(tableName.trim(), field, value, id)) {
				return Constant.SUCCESS;
			}
		}

		return Constant.UNSUCCESS;
	}
}
